package cn.sumpu.app.guoguojie.biz.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import cn.sumpu.app.guoguojie.biz.AuxProfilerBiz;
import cn.sumpu.app.guoguojie.biz.IdentifierBiz;
import cn.sumpu.app.guoguojie.dao.AuxProfilerDao;
import cn.sumpu.app.guoguojie.domain.AuxProfiler;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.IdGenerator;

public class AuxProfilerBizImpl implements AuxProfilerBiz {

	@Resource
	private IdentifierBiz identifierBiz;
	
	private AuxProfilerDao auxProfilerDao;

	public void setAuxProfilerDao(AuxProfilerDao auxProfilerDao) {
		this.auxProfilerDao = auxProfilerDao;
	}

	@Override
	public void profile(AuxProfiler auxProfiler) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("url", auxProfiler.getUrl());
		AuxProfiler original = auxProfilerDao.getByUrl(map);
		if (null == original) {
			auxProfiler.setIdentifier(identifierBiz.generateId("aux_profiler"));
			auxProfiler.setTotalRuntime((long) (auxProfiler.getRuntime()));
			auxProfiler.setTotalView(1);
			auxProfilerDao.insert(auxProfiler);
		} else {
			AuxProfiler newObj = new AuxProfiler(original.getIdentifier(),
					auxProfiler.getIp(), auxProfiler.getUserAgent(),
					auxProfiler.getReferer(), auxProfiler.getUrl(),
					auxProfiler.getRuntime(), auxProfiler.getTotalRuntime(),
					auxProfiler.getTotalView());
			newObj.setTotalRuntime(original.getTotalRuntime()
					+ auxProfiler.getRuntime());
			newObj.setTotalView(original.getTotalView() + 1);
			auxProfilerDao.update(newObj);
		}
	}

}
